#include "DaterMainGroupEventWindow.h"
#include "DaterCommon.h"
#include "DaterBaseWindowManager.h"
#include "DaterSystem.h"

MainGroupEventWindow::MainGroupEventWindow(const CEGUI::String& id_name, CEGUI::Window* parent):
MainWindow(parent)
{
	using namespace CEGUI;

	_root = CEGUI::WindowManager::getSingleton().loadWindowLayout("GroupEventWindow.layout",id_name);

	_name = _root->getName().c_str();

	WindowManager& winMgr = WindowManager::getSingleton();

	ScrollablePane* pane = static_cast<ScrollablePane*>(winMgr.getWindow("GroupEventWindow/ScrollPane"));
	pane->setContentPaneAutoSized(true);
	Window* datePrototype = winMgr.getWindow("GroupEventWindow/ScrollPane/EventDate1");
	Window* timePrototype = winMgr.getWindow("GroupEventWindow/ScrollPane/EventTime1");
	Window* namePrototype = winMgr.getWindow("GroupEventWindow/ScrollPane/EventTime1");
	datePrototype->setRiseOnClickEnabled(false);
	timePrototype->setRiseOnClickEnabled(false);
	namePrototype->setRiseOnClickEnabled(false);

	Window* dateToAdded;
	Window* timeToAdded;
	Window* nameToAdded;
	String dateNewName;
	String timeNewName;
	String nameNewName;
	std::string newText;

	for(int i = 2; i < 10; i++){
		dateNewName = "GroupEventWindow/ScrollPane/EventDate" + PropertyHelper::intToString(i);
		dateToAdded = datePrototype->clone(dateNewName);
		dateToAdded->setYPosition(cegui_reldim(0.1f*(i-1)));
		datePrototype->getParent()->addChildWindow(dateToAdded);

		timeNewName = "GroupEventWindow/ScrollPane/EventTime" + PropertyHelper::intToString(i);
		timeToAdded = timePrototype->clone(timeNewName);
		timeToAdded->setYPosition(cegui_reldim(0.1f*(i-1)));
		timePrototype->getParent()->addChildWindow(timeToAdded);

		nameNewName = "GroupEventWindow/ScrollPane/EventName" + PropertyHelper::intToString(i);
		nameToAdded = namePrototype->clone(nameNewName);
		nameToAdded->setYPosition(cegui_reldim(0.1f*(i-1)));
		namePrototype->getParent()->addChildWindow(nameToAdded);
	}

	refreshEvent();

	subscribeEvents();
}

void MainGroupEventWindow::showWindow()
{
	//_parent->addChildWindow(_root);
	_parent->addChildWindow(_root);
	_root->activate();
}

void MainGroupEventWindow::hideWindow()
{
	//_parent->removeChildWindow(_root);
	_root->deactivate();
	_parent->removeChildWindow(_root);
}

void MainGroupEventWindow::subscribeEvents(){
	using namespace CEGUI;

	Window *nameWindow/*, *createrWindow*/;
	for(int i  = 1; i <= 10; i++)
	{
		nameWindow = WindowManager::getSingleton().getWindow("GroupEventWindow/ScrollPane/GroupName" + PropertyHelper::intToString(i));

		nameWindow->subscribeEvent(Window::EventMouseWheel, Event::Subscriber(&MainGroupEventWindow::MouseWheel, this));

		nameWindow->subscribeEvent(Window::EventMouseClick, Event::Subscriber(&MainGroupEventWindow::MouseClick,this));
	}
}

bool MainGroupEventWindow::MouseClick(const CEGUI::EventArgs& args)
{
	using namespace CEGUI;

	float wheelDistance = static_cast<const CEGUI::MouseEventArgs&>(args).wheelChange;

	static_cast<ScrollablePane*>(_root->getChildAtIdx(0))->setVerticalScrollPosition(
		static_cast<ScrollablePane*>(_root->getChildAtIdx(0))->getVerticalScrollPosition() - wheelDistance/27);

	return true;
}

bool MainGroupEventWindow::MouseWheel(const CEGUI::EventArgs& args)
{

	return true;
}

void MainGroupEventWindow::refreshEvent()
{
}